<template>
  <a-modal
    title="查看采购订单"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="订单编码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入采购订单编码" v-decorator="['code', {rules: [{required: false, message: '请输入采购订单编码！'}]}]" :readOnly="true" />
        </a-form-item>
        <a-form-item
          label="到货时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker
            style="width: 100%"
            :show-time="{ format: 'HH:mm:ss' }"
            format="YYYY-MM-DD HH:mm:ss"
            placeholder=""
            v-decorator="['arrivalTime',{rules: [{ required: true, message: '请选择到货时间！' }]}]"
            disabled="disabled"
          />
        </a-form-item>
        <a-form-item
          label="订单来源"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入订单来源" v-decorator="['orderSourceName', {rules: [{required: true, message: '请输入订单来源！'}]}]" :readOnly="true" />
        </a-form-item>
        <puor-order-table-form-item
          :dataSource="puorDetailList"
          :proList="list.proList"
          :purchaseNum="form.getFieldValue('purchaseNum') ? form.getFieldValue('purchaseNum') : null"
          :unitPrice="form.getFieldValue('unitPrice') ? form.getFieldValue('unitPrice') : null"
          :totalPrice="form.getFieldValue('totalPrice') ? form.getFieldValue('totalPrice') : null"
          :disabled="true"
          @add="
            ({ tableData }) => {
              puorDetailList = tableData
            }
          "
          @del="
            ({ tableData }) => {
              puorDetailList = tableData
            }
          "
        />
        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea
            :rows="3"
            placeholder="请输入备注信息"
            v-decorator="['remarks']"
            :readOnly="true"
          />
        </a-form-item>
      </a-form>
    </a-spin>
    <template slot="footer">
      <a-button @click="handleCancel">关闭</a-button>
    </template>
  </a-modal>
</template>

<script>
import puorOrderTableFormItem from '@/views/main/puororder/puorOrderTableFormltem'
import { proList } from '@/api/modular/main/pro/proManage'
import moment from 'moment'
export default {
  components: {
    puorOrderTableFormItem
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      list: {
        // 产品
        proList: []
      },
      puorDetailList: [],
      arrivalTimeDateString: '',
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    moment,
    // 初始化方法
    open(record) {
      this.visible = true
      this.puorDetailList = record.puorDetailList
      proList().then((res) => {
        this.list.proList = res.data
      })
      setTimeout(() => {
        this.form.setFieldsValue({
          id: record.id,
          code: record.code,
          orderSourceName: record.orderSourceName,
          remarks: record.remarks
        })
      }, 100)
      // 时间单独处理
      if (record.arrivalTime) {
        this.form.getFieldDecorator('arrivalTime', { initialValue: moment(record.arrivalTime, 'YYYY-MM-DD HH:mm:ss') })
        this.arrivalTimeDateString = moment(record.arrivalTime).format('YYYY-MM-DD HH:mm:ss')
      }
    },
    handleSubmit() {
      this.form.resetFields()
      this.visible = false
      this.$emit('ok')
    },
    handleCancel () {
      this.arrivalTimeDateString = ''
      this.form.getFieldDecorator('arrivalTime', { initialValue: null })
      this.form.resetFields()
      this.visible = false
      this.$emit('ok')
    }
  }
}
</script>
